﻿using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System;
using Terraria.ID;
using Terraria;
using Terraria.ModLoader;
using EAS.Common.Players;

namespace EAS.Content.Projectiles
{
    public class Probe : ModProjectile
    {
        public override void SetDefaults()
        {
            Projectile.width = 32;
            Projectile.height = 32;
            Projectile.aiStyle = 66;
            Projectile.friendly = true;
            Projectile.hostile = false;
            Projectile.timeLeft = 2;
            Projectile.ignoreWater = true;
            Projectile.tileCollide = false;
            Projectile.extraUpdates = 1;
            AIType = 20;
        }
        public override bool PreDraw(ref Color lightColor)
        {
            Texture2D texture = ModContent.Request<Texture2D>(Texture, (ReLogic.Content.AssetRequestMode)2).Value;
            Rectangle frame = texture.Frame(1, Main.projFrames[Type], 0, Projectile.frame, 0, 0);
            Vector2 origin = frame.Size() * 0.5f;
            Vector2 drawPosition = Projectile.Center - Main.screenPosition;
            SpriteEffects direction = (SpriteEffects)(Projectile.spriteDirection == 1 ? 1 : 0);
            for (int i = 0; i < Projectile.oldPos.Length; i++)
            {
                float afterimageFade = 1f - i / (float)Projectile.oldPos.Length;
                Color color = Color.Lerp(Color.Red, Color.Red, afterimageFade);
                color.A = 25;
                Color afterimageDrawColor = color * Projectile.Opacity * afterimageFade * 0.6f;
                Vector2 afterimageDrawPosition = Projectile.oldPos[i] + Projectile.Size * 0.5f - Main.screenPosition;
                Main.EntitySpriteDraw(texture, afterimageDrawPosition, new Rectangle?(frame), afterimageDrawColor, Projectile.rotation, origin, Projectile.scale, direction, 0);
            }
            for (int j = 0; j < 8; j++)
            {
                Color color = Color.Red;
                color.A = 25;
                Color afterimageDrawColor2 = color * Projectile.Opacity * 0.4f;
                Vector2 afterimageDrawPosition2 = Projectile.Center - Main.screenPosition + (6.2831855f * j / 8f).ToRotationVector2() * 3f;
                Main.EntitySpriteDraw(texture, afterimageDrawPosition2, new Rectangle?(frame), afterimageDrawColor2, Projectile.rotation, origin, Projectile.scale, direction, 0);
            }
            Main.EntitySpriteDraw(texture, drawPosition, new Rectangle?(frame), Projectile.GetAlpha(lightColor), Projectile.rotation, origin, Projectile.scale, direction, 0);
            return false;
        }
        public Player Owner => Main.player[Projectile.owner];
        public float HoverOffsetInterpolant
        {
            get
            {
                float projectileCounts = Owner.ownedProjectileCounts[Type];
                if (projectileCounts <= 1f)
                {
                    return 0.5f;
                }
                return HoverOffsetIndex / (projectileCounts - 1f);
            }
        }
        public int HoverOffsetIndex => (int)Projectile.ai[0];

        public ref float Timer => ref Projectile.ai[1];
        public void HoverInPlace()
        {
            Vector2 hoverDestination = Owner.Top + new Vector2(MathHelper.Lerp(-100f, 100f, HoverOffsetInterpolant), -80f);
            hoverDestination.Y += ((float)Math.Sin((double)(6.2831855f * HoverOffsetInterpolant + Timer / 50f)) * 0.5f + 0.5f) * 40f;
            Projectile.Center = Vector2.Lerp(Projectile.Center, hoverDestination, 0.02f).MoveTowards(hoverDestination, 8f);
        }
        public override void AI()
        {
            Player player = Main.player[Projectile.owner];
            if (player.GetModPlayer<AccPlayer>().probes)
            {
                Projectile.timeLeft = 2;
            }
            if (attackCooldown > 0)
            {
                attackCooldown--;
            }
            if (player.HasMinionAttackTargetNPC)
            {
                Vector2 targetDir = Main.npc[player.MinionAttackTargetNPC].Center - Projectile.Center;
                Projectile.rotation = targetDir.ToRotation() + MathHelper.ToRadians(90f);
                Projectile.direction = Projectile.spriteDirection = targetDir.X > 0f ? 1 : -1;
                targetDir.Normalize();
                if (attackCooldown <= 0)
                {
                    int a = Projectile.NewProjectile(null, Projectile.Center.X, Projectile.Center.Y, targetDir.X * 10f, targetDir.Y * 10f, ProjectileID.PinkLaser, 66, 0f, 0, player.whoAmI, 0f);
                    Main.projectile[a].friendly = true;
                    Main.projectile[a].hostile = false;
                    Main.projectile[a].timeLeft = 120;
                    Main.projectile[a].tileCollide = false;
                    attackCooldown = 20 + player.statLife / 5;
                }
            }
            if (!player.HasMinionAttackTargetNPC)
            {
                Projectile.direction = Projectile.spriteDirection = Projectile.velocity.X > 0f ? 1 : -1;
                Projectile.rotation = Projectile.velocity.ToRotation() + MathHelper.ToRadians(90f);
            }
        }
        public override bool? CanDamage()
        {
            return true;
        }

        private int attackCooldown;
    }
}
